// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'

Vue.config.productionTip = false


//创建axios对象
let http = axios.create();
//请求拦截器:会在每一次真的发送请求之前执行，在这里将token设置到请求头
//达到每次请求自定携带token
http.interceptors.request.use(
  config => {
    console.log("发送请求之前执行");
    //1.得到token
    let token = window.sessionStorage.getItem("token");
    //2.判断:如果不为空，则设置到请求头
    if(token){
      config.headers.authorization = token;
    }
    //放行请求
    return config;
  }
);

//响应拦截器 : 判断响应头是否携带token
http.interceptors.response.use(
  response => {
    console.log("得到响应之后执行");
    //1.从响应头获取token
    let token = response.headers.authorization;
    //2.判断是否存在，有就存起来
    if(token){
      window.sessionStorage.setItem("token",token);
    }
    //
    return response;
  }
);




//设置为属性
Vue.prototype.axios = http;
//设置URL
Vue.prototype.baseUrl = 'http://localhost:8080/';

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})